﻿using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using EyFramework.Common;
using EyFramework.Sys.Core.Service;
using EyFramework.Sys.Entity;
using EyFramework.ThirdPart.RedisUtil;
using Newtonsoft.Json;

namespace EyFramework.Task
{
    /// <summary>
    /// 对应的任务卡片
    /// </summary>
    public partial class TaskCard : DevExpress.XtraEditors.XtraUserControl
    {
        private QrtzCsTask _qrtzCsTask;

        private IQrtzCsTaskService qrtzCsTaskService;

        public TaskCard()
        {
            InitializeComponent();
        }

        public TaskCard(QrtzCsTask qrtzCsTask)
        {
            InitializeComponent();
            _qrtzCsTask = qrtzCsTask;
        }

        private void peMore_Click(object sender, EventArgs e)
        {
            popupMenu1.ShowPopup(Control.MousePosition);
        }

        private void peStart_Click(object sender, EventArgs e)
        {
            //启动任务
            StartTask();
            SetBtnState(TaskConstants.TASK_COMMAND_启动);
        }

        private void SetBtnState(string taskState)
        {
            //已启动
            if (taskState == TaskConstants.TASK_COMMAND_启动)
            {
                peStart.Enabled = false;
                pePause.Enabled = true;
                peStop.Enabled = true;
            }
            else if (taskState == TaskConstants.TASK_COMMAND_暂停)
            {
                peStart.Enabled = true;
                pePause.Enabled = false;
                peStop.Enabled = true;
            }
            else
            {
                peStart.Enabled = true;
                pePause.Enabled = false;
                peStop.Enabled = false;
            }
        }

        private void StartTask()
        {
            TaskCommand command = new TaskCommand()
            {
                TaskId = _qrtzCsTask.TASK_ID.ToString(),
                CommandType = TaskConstants.TASK_COMMAND_启动,
                CommandTime = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
            };
            //先调度器发送启动任务的命令
            RedisHelper.Publish(Constants.TASK_CHANNEL, JsonConvert.SerializeObject(command));
            Thread.Sleep(3000);
            _qrtzCsTask = qrtzCsTaskService.GetTaskById(_qrtzCsTask.TASK_ID.ToString());
            ShowData();
        }

        private void pePause_Click(object sender, EventArgs e)
        {
            TaskCommand command = new TaskCommand()
            {
                TaskId = _qrtzCsTask.TASK_ID.ToString(),
                CommandType = TaskConstants.TASK_COMMAND_暂停,
                CommandTime = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
            };
            //先调度器发送暂停任务的命令
            RedisHelper.Publish(Constants.TASK_CHANNEL, JsonConvert.SerializeObject(command));
        }

        private void peStop_Click(object sender, EventArgs e)
        {
            TaskCommand command = new TaskCommand()
            {
                TaskId = _qrtzCsTask.TASK_ID.ToString(),
                CommandType = TaskConstants.TASK_COMMAND_停止,
                CommandTime = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
            };
            //先调度器发送停止任务的命令
            RedisHelper.Publish(Constants.TASK_CHANNEL, JsonConvert.SerializeObject(command));
        }

        private void bbiEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //修改任务
            FrmTaskEdit frmTask = new FrmTaskEdit(_qrtzCsTask.TASK_ID.ToString());
            if (frmTask.ShowDialog() == DialogResult.OK)
            {
                this._qrtzCsTask = qrtzCsTaskService.GetTaskById(_qrtzCsTask.TASK_ID.ToString());
            }
        }

        private void bbiDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //删除任务
        }

        private void bbiLog_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //查看执行日志
        }

        private void TaskCard_Load(object sender, EventArgs e)
        {
            qrtzCsTaskService = Globals.GetService<IQrtzCsTaskService>();
            peCover.Visible = (_qrtzCsTask == null);
            ShowData();
        }

        private void ShowData()
        {
            if (_qrtzCsTask != null)
            {
                this.lblTaskName.Text = _qrtzCsTask.TASK_NAME;
                this.lblTaskInfo.Text = _qrtzCsTask.TASK_INFO;
                this.lblCron.Text = _qrtzCsTask.CRON;
                SetRunState();
                this.lblTaskType.Text = _qrtzCsTask.TASK_TYPE;
            }
        }

        /// <summary>
        /// 根据当前任务的状态设置运行状态
        /// </summary>
        private void SetRunState()
        {
            if (_qrtzCsTask.TASK_RUN_STATUS == TaskConstants.RUN_STATE_暂停)
            {
                //暂停
                lblState.Text = "已暂停";
                lblState.BackColor = Color.Crimson;
                peStart.Enabled = true;
                pePause.Enabled = false;
                peStop.Enabled = true;
            }
            else if (_qrtzCsTask.TASK_RUN_STATUS == TaskConstants.RUN_STATE_运行中)
            {
                //准备就绪阶段
                lblState.Text = "运行中";
                lblState.BackColor = Color.ForestGreen;
                peStart.Enabled = false;
                pePause.Enabled = true;
                peStop.Enabled = true;
            }
            else
            {
                //准备就绪阶段
                lblState.Text = "未启动";
                lblState.BackColor = Color.DodgerBlue;
                peStart.Enabled = true;
                pePause.Enabled = false;
                peStop.Enabled = false;
            }
        }

        private void peCover_Click(object sender, EventArgs e)
        {
            FrmTaskEdit frmTask = new FrmTaskEdit("");
            frmTask.ShowDialog();
        }

        public void RefreshData()
        {
        }
    }
}